package com.citic.netty.server.handler;

import com.citic.netty.common.protocol.request.LogoutRequestPacket;
import com.citic.netty.common.protocol.response.LogoutResponsePacket;
import com.citic.netty.common.protocol.session.Session;
import com.citic.netty.common.utils.SessionUtil;

import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;

@ChannelHandler.Sharable
public class LogoutRequestHandler extends SimpleChannelInboundHandler<LogoutRequestPacket>{

    public static final LogoutRequestHandler INSTANCE = new LogoutRequestHandler();

    private LogoutRequestHandler() {

    }
    
	@Override
	protected void channelRead0(ChannelHandlerContext ctx, LogoutRequestPacket msg) throws Exception {
		Session session = SessionUtil.getSession(ctx.channel());
		System.err.println(session.getUsername() + "登出成功");
		
		SessionUtil.unBindSession(ctx.channel());
        LogoutResponsePacket logoutResponsePacket = new LogoutResponsePacket();
        logoutResponsePacket.setSuccess(true);
        
        ctx.channel().writeAndFlush(logoutResponsePacket);
	}

}
